package cn.wgx.modules.sys.util;

import cn.wgx.commons.util.SpringContextHolder;
import cn.wgx.modules.base.entity.BaseFavoriteAndLikeEntity;
import cn.wgx.modules.sys.entity.shortentity.ShortSysUser;
import cn.wgx.modules.sys.services.SysUserFavoriteService;
import cn.wgx.modules.sys.services.SysUserLikeService;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏和点赞工具类
 */
public class FavoriteAndLikeUtil {

    private static SysUserLikeService sysUserLikeService;

    private static SysUserFavoriteService sysUserFavoriteService;

    static {
        sysUserLikeService = SpringContextHolder.getBean("sysUserLikeService");
        sysUserFavoriteService = SpringContextHolder.getBean("sysUserFavoriteService");
    }

    /**
     * @param targetList 目标数组
     * @param type       类型: //1课程,2作业,3笔记,4关注的人
     * @param idName     目标主键ID名称, 1. course_id, 2.assignment_his_id, 3.course_note_id 4.user_id
     * @param user_id    当前用户ID
     */
    public static List wrapFavAndLike(List<? extends BaseFavoriteAndLikeEntity> targetList, Integer type, String idName, Integer user_id) {
        sysUserFavoriteService.wrapFavorite(targetList, type, idName, user_id);
        sysUserLikeService.wrapLike(targetList, type, idName, user_id);
        return targetList;
    }


    public static List wrapFavAndLike(List<? extends BaseFavoriteAndLikeEntity> targetList, Integer type) {
        String idName = null;
        switch (type) {
            case 1:
                idName = "course_id";
                break;
            case 2:
                idName = "assignment_his_id";
                break;
            case 3:
                idName = "course_note_id";
                break;
            case 4:
                idName = "user_id";
                break;
            default:
                return targetList;
        }
        Integer user_id = UserUtil.getUser().getUser_id();
        wrapFavAndLike(targetList, type, idName, user_id);
        return targetList;
    }

    public static BaseFavoriteAndLikeEntity wrapFavAndLike(BaseFavoriteAndLikeEntity targetList, Integer type) {
        if(targetList == null){
            return targetList;
        }
        String idName = null;
        switch (type) {
            case 1:
                idName = "course_id";
                break;
            case 2:
                idName = "assignment_his_id";
                break;
            case 3:
                idName = "course_note_id";
                break;
            case 4:
                idName = "user_id";
                break;
            default:
                return targetList;
        }
        Integer user_id = UserUtil.getUser().getUser_id();
        List list = new ArrayList();
        list.add(targetList);
        wrapFavAndLike(list, type, idName, user_id);
        return targetList;
    }
    public static ShortSysUser wrapFollow(ShortSysUser shortSysUser) {
        sysUserFavoriteService.wrapFollow(shortSysUser, UserUtil.getUser().getUser_id());
        return shortSysUser;
    }

}
